//
//  ChooseComTypeView.m
//  WeCommunity
//
//  Created by mac on 2017/6/14.
//  Copyright © 2017年 lzy. All rights reserved.
//

#import "ChooseComTypeView.h"


static CGFloat ButtonWidth = 60;


@interface ChooseComTypeView ()

@property (nonatomic,strong) UIScrollView *scrollView;

@property (nonatomic,strong) UIView *indicatorView;

@property (nonatomic,strong) NSMutableArray *channelBtnArray;

@property (nonatomic, weak) UIButton *lastSelectedButton;

@end

@implementation ChooseComTypeView
#pragma mark - get

-(UIScrollView *)scrollView
{
    if(!_scrollView){
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.frame = CGRectMake(0, 0, CGRectGetWidth(self.bounds),CGRectGetHeight(self.bounds));
        _scrollView.showsHorizontalScrollIndicator = NO;
    }
    return _scrollView;
}

-(UIView *)indicatorView
{
    if (!_indicatorView) {
        _indicatorView = [[UIView alloc] init];
        _indicatorView.backgroundColor = TINT_GREEN_COLOR;
    }
    return _indicatorView;
}

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        [self initialization];

    }
    return self;
}

#pragma mark 初始化子控件
- (void)initialization
{
    self.backgroundColor = WHITE_COLOR;
    
    [self addSubview:self.scrollView];
    
    [self.scrollView addSubview:self.indicatorView];
    
    UIView * lineView = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(self.bounds) - 1, CGRectGetWidth(self.bounds), 1)];
    lineView.backgroundColor = LINE_COLOR;
    [self addSubview:lineView];
}

-(void)setChannelNameArray:(NSArray *)channelNameArray
{
    _channelNameArray = channelNameArray;
    self.channelBtnArray = [NSMutableArray array];
    
    self.scrollView.contentSize = CGSizeMake(ButtonWidth * channelNameArray.count, 0);
    for (NSInteger i = 0; i < channelNameArray.count; i++) {
        UIButton * button = [self createChannelButton];
        button.frame = CGRectMake(i * ButtonWidth, 0, ButtonWidth, self.frame.size.height);
        [button setTitle:channelNameArray[i] forState:UIControlStateNormal];
        [self.scrollView addSubview:button];
        
        [self.channelBtnArray addObject:button];
    }
    
    [self.scrollView bringSubviewToFront:self.indicatorView];
    
    //默认选中第一个
    [self clickChannelButton:self.channelBtnArray[0]];
}

#pragma mark 创建ChannelButton
- (UIButton *)createChannelButton{
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
    [button setTitleColor:TINT_GREEN_COLOR forState:UIControlStateDisabled];
    [button.titleLabel setFont:PFR_FONT(14)];
    [button addTarget:self action:@selector(clickChannelButton:) forControlEvents:UIControlEventTouchUpInside];
    [button layoutIfNeeded];
    button.exclusiveTouch = YES;
    return button;
}

#pragma mark 选择了某个ChannelButton
- (void)clickChannelButton:(UIButton *)sender
{
    self.lastSelectedButton.titleLabel.font = PFR_FONT(14);
    self.lastSelectedButton.enabled = YES;
    self.lastSelectedButton = sender;
    self.lastSelectedButton.enabled = NO;
    //选中的标签要居中，也就是scrollView的offset.x加屏幕的一半要等于标签的中心
    CGFloat newOffsetX = sender.center.x - CGRectGetWidth(self.bounds) * 0.5;
    if (newOffsetX > self.scrollView.contentSize.width - self.scrollView.frame.size.width) {
        newOffsetX = self.scrollView.contentSize.width - self.scrollView.frame.size.width;
    }
    if (newOffsetX < 0) {
        newOffsetX = 0;
    }
    [UIView animateWithDuration:0.25 animations:^{
        [sender.titleLabel setFont:PFR_FONT(16)];
        [sender layoutIfNeeded];
        [self.scrollView setContentOffset:CGPointMake(newOffsetX, 0)];
        //indicatorView宽度会比titleLabel窄20，centerX与titleLabel相同
        self.indicatorView.frame = CGRectMake(sender.frame.origin.x + sender.titleLabel.frame.origin.x - 10, self.frame.size.height - 4, sender.titleLabel.frame.size.width + 20, 4);
    }];
    if ([self.delegate respondsToSelector:@selector(chooseChannelWithIndex:)]) {
        NSInteger index = [self.channelBtnArray indexOfObject:sender];
        [self.delegate chooseChannelWithIndex:index];
    }
}

#pragma mark 选中某个ChannelButton
- (void)didSelectChannelButtonWithIndex:(NSInteger)index
{
    [self clickChannelButton:self.channelBtnArray[index]];
}



@end
